Home |
| Latest | About | Random
# Week 8 Friday Dear Class, Today we discuss `const` instances and member functions marked `const` **(READ: Sections 15.4, 15.5)**. Some road map ahead. - Next week (Week 9) we plan to finish sections 15 and 16. There is no class Friday, but there is class Monday and Wednesday. - Last week (Week 10) we plan to do **section 17.1, 17.2, 17.4 on pointers**, and **section 18.1, 18.2, 18.3 on C-style arrays**. - Final exam is Monday 12/7. Please check schedule of classes for location. - There is a completion based pHW5 posted, there will be another homework set to be posted. -- B. . . . . . ## Story so far with `struct` Here is the story so far with `struct` that you should be familiar with... - **(15.1)** A `struct` is a custom data type that can have **member variables**, so it acts as a container for data. It acts as a blueprint for this data type, and if we have `struct C`, we can create an **instance** of it by calling `C a;`, where `a` is the name of an instance of `struct C`. - **(15.3)** When a `struct` is created, its **constructor** is called. If there is no user-defined constructor provided then we have a compiler generated default constructor. The default constructor is one that has no explicit parameters. We can use a **member initializer list** to initialize member variables. When the constructor is called, an **implicit parameter** of reference type that we call `self` is created in the constructor scope, which we pass in the **implicit argument** of the instance that is calling it. - **(15.4)** A `struct` can also have **member functions**. When a member function is called, an **implicit parameter** of reference type is also created, which we pass in the **implicit argument** of the instance that is calling it. If the member function is **marked `const`**, then this implicit parameter is a reference to `const`. - **(15.7)** We can also **define member functions and constructor** outside of a `struct`'s interface, however you will need to (1) declare it inside the interface first (and preserve `const` mark, if any), and (2) defining outside by using its full name, like `C::f`, where `f` is a member function of `struct C`. Ok. Let us now discuss how various `const` correctness issue arises when dealing with `struct`. (Please read 15.4 and 15.5!) . . . . . ## Compiler generated default constructor and `const` instances. If a `struct` does not have a user-defined constructor, then the compiler generates a **default constructor**, which is a constructor that takes no explicit parameters. However if a `struct` with a compiler generated default constructor has a member variable of fundamental type that is left uninitialized (no default member initializer like `int x = 0;`), then we cannot create a `const` instance of this `struct`. **Example.** Here line 6 gives a build error: ```cpp struct C { int x; }; int main () { C a; const C b; return 0; } ``` So if we want a `const` instance of a struct with fundamental type member variables, then we need to provide a user-defined constructor (or give every such member variable a default member initializer, e.g. `int x = 0;`). **Example.** Now this works. ```cpp struct C { int x; C () {} }; int main () { C a; const C b; return 0; } ``` . . . . . ## Fact 1. A `const` instance of a `struct` has all member variables `const`. Now say we have a `const` instance of a `struct`, then all its member variables become `const`, and cannot be modified. **Example.** Here line 9 is a **build error**, as the instance `b` is `const`, its member variable `b.x` is also `const`, but we tried to modify `b.x` on line 9. ```cpp struct C { int x; C () {} }; int main () { C a; a.x = 10; const C b; b.x = 10; return 0; } ``` . . . . . ## Fact 2. A member function marked `const` makes the implicit parameter a reference to `const`. Recall when calling a member function of a `struct`, an implicit parameter of reference type (that we call `self`, for our PIC10A class) is created in our member function scope, and it is bound to the instance calling it. Say `C::f` is a member function of `struct C`. Then... - If `C::f` is not marked `const`, then `self` is a regular reference (think of it of type `C&`) - If `C::f` is marked `const`, then `self` is a reference to `const` (think of it of type `const C&`) So, **a member function should be marked** `const` **when there is no way it can be responsible for** **mutating the instance that is referenced by the implicit parameter.** **Example.** Here line 8 gives a build error. Draw a diagram to see why! ```cpp struct C { void f () {} }; int main () { C a; const C b; a.f(); b.f(); return 0; } ``` Here the member function `C::f` is not marked `const`, so when we call `b.f()`, we create an implicit parameter `self` that is a non-`const` reference. However, we then try to assign to `self` the implicit argument `b`, which is `const`. This violates `const`-correctness, we cannot use a non-`const` reference to reference a `const`! . . . . . **Example.** Now, `C::f` is marked `const`. So calling `C::f` by any instance would create the implicit parameter `self` as a reference to `const`, which can be used to reference both `const` and non-`const` implicit arguments. ```cpp struct C { void f () const {} }; int main () { C a; const C b; a.f(); b.f(); return 0; } ``` . . . . . **Example.** Here line 16 is a build error. Draw a diagram to see why! ```cpp #include using namespace std; struct Point { double x, y; Point (double _x, double _y) : x(_x), y(_y) {} void print() { cout << '(' << x << ", " << y << ')' << endl; } }; int main () { Point a(1, 2); const Point b(3, 4); a.print(); b.print(); return 0; } ``` . . . . . **Example.** Here line 5 is a build error. The member function `C::f` is marked `const`, so the implicit parameter `self` is a reference to `const`, but we attempt to modify the value of `x` through this reference to `const`. Draw a diagram! ```cpp struct C { int x; C () {} void f() const { x++; } }; int main () { return 0; } ``` . . . . . **Example.** The following has a build error on line 4. Draw a diagram! ```cpp struct C { void f () {} void g () const { f(); } }; int main () { return 0; } ``` If one is to call `C::g`, then an implicit parameter of type `const C&` is bound to the implicit argument (which is the caller instance itself). Then if one executes the body of `C::g`, which calls `C::f`, another implicit parameter of type `C&` is to be bound to the object referenced by the `const C&` implicit parameter `self`, which is not allowed! . . . . . ## Some consequences of Fact 1 and Fact 2. To recap, we have - **Fact 1.** If we have a `const` instance of a `struct`, then all member variables are `const` - **Fact 2.** If a member function of a `struct` is marked `const`, then when called, the implicit parameter is a reference to `const`. If not marked `const`, then the implicit parameter is just a reference. From these, we can synthesize the following consequences, which can be derived from both facts above: - It is a build error for a `const` instance of a `struct` to call a member function that is not marked `const`. - If a member function is marked `const`, then within the function body it is a build error to assign to a member variable of the implicit parameter of fundamental type (and often true of class type too). - If a member function is marked `const`, then within the function body it is a build error to call a member function which is not marked `const` through the implicit parameter. - If a member function of a `struct` is not marked `const`, then we cannot call it from a `const` instance of the `struct`. Now go back to the examples above, and see which examples can be explained by one of these consequences! . . . . . ## Practice. Identify all the lines with errors. Comment all the lines with errors out. Then what is the output of the code? ```cpp #include using namespace std; struct C { int var; C() : var(0) {} void f() { ++var; cout << "no const: " << var << endl; } void g() const { cout << " const: " << var << endl; } void h() const; }; int main() { C a; a.var = 1; a.f(); a.g(); const C b; b.var = 2; b.f(); b.g(); return 0; } void C::h() const { var = 4; f(); g(); } ```